package org.ailoli.aixiang.ui

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import kotlinx.android.synthetic.main.activity_main.*
import org.ailoli.aixiang.R
import org.ailoli.aixiang.adapter.BatteryAdapter
import org.ailoli.aixiang.base.BaseMvpActivity
import org.ailoli.aixiang.bean.BatteryBean
import org.ailoli.aixiang.bean.CabinetBean
import org.ailoli.aixiang.bean.LoginBean
import org.ailoli.aixiang.contract.MainContract
import org.ailoli.aixiang.data.BatteryData
import org.ailoli.aixiang.present.MainPresenter
import org.ailoli.aixiang.utils.Utils

class MainActivity : BaseMvpActivity<MainPresenter>(), MainContract.View {
    @RequiresApi(Build.VERSION_CODES.R)

    var batteryAdapter: BatteryAdapter? = null
    var batteryList: ArrayList<BatteryBean>? = null
    override fun showLoading() {
    }

    override fun hideLoading() {
    }

    override fun onError(throwable: Throwable?) {
    }

    override fun setQrCodeUrl(url: String) {
        Log.e("success: ", url)
        val QrCodeImg = Utils.createQRCodeBitmap(
            url,
            800,
            800,
            "UTF-8",
            "H",
            "1",
            Color.BLACK,
            Color.WHITE
        )
        val progresslayoutParams = loading_progress.layoutParams
        progresslayoutParams.height = 0
        loading_progress.layoutParams = progresslayoutParams
        val qrcodeImgLayoutParams = huandian_img.layoutParams
        qrcodeImgLayoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
        huandian_img.layoutParams = qrcodeImgLayoutParams
        huandian_img.setImageBitmap(QrCodeImg)
    }

    override fun loginCallBack(loginBean: LoginBean) {
        prefs.edit().putString("token", loginBean.access_token).apply()
        prefs.edit().putString("expires_in", loginBean.expires_in).apply()
        mPresenter!!.getQrCodeUrl("10000000")
    }

    override fun onOneBatteryUpdate(index: Int, batteryData: BatteryData) {
//        Log.e((index + 1).toString() + "号电池", batteryData.batterySoc.toString())
//        if (index < 0 || index >= batteryList!!.size) return
//        val tempBean = batteryList!!.get(index)
//        tempBean.batteryData = batteryData
//        batteryList!!.set(index, tempBean)
//        batteryGridView.adapter = BatteryAdapter(this, batteryList!!)
    }

    override fun onOneBatteryComplete(batteryList: ArrayList<BatteryBean>) {
//        mPresenter!!.updateAllBattery(this, batteryList!!)
        batteryGridView.adapter = BatteryAdapter(this, batteryList)
    }

    override fun onFetchCabinetLockComplete(cabinetBean: CabinetBean) {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.decorView.apply {
            systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//            windowInsetsController?.also {
//                it.hide(WindowInsets.Type.statusBars())
//                it.hide(WindowInsets.Type.navigationBars())
//            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initView() {
        mPresenter = MainPresenter()
        mPresenter!!.attachView(this)
        mPresenter!!.getAllSerialPort()
        open_door.setOnClickListener {
            mPresenter!!.openDoor()
        }
        close_door.setOnClickListener {
            mPresenter!!.closeDoor()
        }
//        mDeviceConnection.claimInterface(usbDevice.getInterface())
//        mPresenter!!.sendDataToSerialPort()
        val context = this
//        先初始化usb口
        mPresenter!!.initUsb(context)
//        然后发送数据
//        mPresenter!!.sendDataToUsb(this)
//        Log.e("test", )
        mPresenter!!.login("cabinet_desktop", "vKgDngF5cRjnwk9byDzftgqMycF8Cf8q")
        batteryList = ArrayList<BatteryBean>()
        batteryList!!.add(BatteryBean(1, null))
        batteryList!!.add(BatteryBean(2, null))
        batteryList!!.add(BatteryBean(3, null))
        batteryList!!.add(BatteryBean(4, null))
        batteryList!!.add(BatteryBean(5, null))
        batteryList!!.add(BatteryBean(6, null))
        batteryList!!.add(BatteryBean(7, null))
        batteryList!!.add(BatteryBean(8, null))
        batteryList!!.add(BatteryBean(9, null))
        batteryGridView.post {
            batteryAdapter = BatteryAdapter(this, batteryList!!)
            batteryAdapter!!.setGridView(batteryGridView)
            batteryGridView.adapter = batteryAdapter
        }
        huandian_img.post {
            mPresenter!!.updateAllBattery(context, batteryList!!)
        }
        huandian_img.setOnClickListener {
//            val systemService = getSystemService(Context.USB_SERVICE)
//            val manager = getSystemService(USB_SERVICE) as UsbManager
//            val deviceList = manager.deviceList
//            Log.e("deviceList", deviceList.size.toString())
//            Log.e("获取完成", "获取完成")
//            val batteryData = BatteryData(20, false)
//            batteryAdapter!!.updateOne(0,batteryData)
//            batteryAdapter!!.notifyDataSetChanged()
        }
    }
}